package com.hqyj.cl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.cl.mapper.UserMapper;
import com.hqyj.cl.pojo.User;
import com.hqyj.cl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    //自动装配
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HttpSession session;

    @Override
    public String login(String username, String password) {
//        调用mapper接口,操作数据库,查询出登录的用户信息
        User user = userMapper.login(username, password);
//        判断当前登录的用户是否存在
        if(user != null){
//            把当前用户存入session中
            session.setAttribute("loginUsername",user.getUsername());
//            判断用户是否为管理员
            if("管理员".equals(user.getRole())){
                return "index";
            }else{
                return "shouye";//疫情界面
            }
        }else {
            return "login";//登录界面
        }
//       获取到用户的角色
    }

    @Override
    public PageInfo<User> selectAllUser(int PageNum,int pageSize) {
        PageHelper.startPage(PageNum, pageSize);
        List<User> userList =userMapper.selectAllUser();
        PageInfo<User> userPageInfo =new PageInfo<>(userList);
        return userPageInfo;
    }

    @Override
    public  Map<String, String> deleteUserById(int id) {
        int i=userMapper.deleteUserById(id);
        Map<String, String> map=new HashMap<>();
        if(i>0){
            map.put("deleteInfo","删除成功");
        }else{
            map.put("deleteInfo","删除失败");
        }
        return map;
    }

    @Override
    public PageInfo<User> selectUserByUserName(int PageNum,int pageSize,String username) {
        PageHelper.startPage(PageNum, pageSize);
        List<User> userList =userMapper.selectUserByUserName(username);
        return new PageInfo<>(userList);
    }

//    updateUserByUsername
    @Override
    public Map<String, String> updateUserByUsername(User user) {

      int i=userMapper.updateUserByUsername(user);
        Map<String, String> map=new HashMap<>();
        if(i>0){
            map.put("updateInfo","修改成功");
        }else{
            map.put("updateInfo","修改失败");
        }
        return map;
    }

    @Override
    public Map<String, String> addUser(User user) {
        int i=userMapper.addUser(user);

        Map<String, String> map=new HashMap<>();
        if(i>0){
            map.put("addInfo","添加成功");
        }else{
            map.put("addInfo","添加失败");
        }
        return map;
    }

    @Override
    public Map<String, String> checkUsername(String username) {
        User user = userMapper.checkUsername(username);
        Map<String, String> map=new HashMap<>();
        if(user!=null){
            map.put("checkInfo","查找成功");
        }else{
            map.put("checkInfo","查找失败");
        }
        return map;
    }


}
